﻿using System;
using System.Collections.Generic;
using System.Linq;
using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000093 RID: 147
    public static class YoLiPaiPayProxy {
        // Token: 0x06000554 RID: 1364 RVA: 0x0000525C File Offset: 0x0000345C
        public static void AddYouLiPaiPayProxyyHandler(this HttpServer svr) {
            svr.RouterList.Add("POST/weixin/init/executeProviderOrder", new HttpContextActionDelegate(Process));
            svr.RouterList.Add("POST/weixin/search/order", new HttpContextActionDelegate(Process));
        }

        // Token: 0x06000555 RID: 1365 RVA: 0x00011470 File Offset: 0x0000F670
        private static void Process(HttpContext ctx) {
            YoliPaiPayResult yoliPaiPayResult = new YoliPaiPayResult();
            try {
                Dictionary<string, string> form = ctx.Request.Form;
                yoliPaiPayResult.data.user_no = form.ReadOrDefault("userNo");
                yoliPaiPayResult.data.order_no = form.ReadOrDefault("orderNo");
                yoliPaiPayResult.data.trx_no = form.ReadOrDefault("trxNo");
                HongshPayTrade hongshPayTrade;
                if (string.IsNullOrEmpty(yoliPaiPayResult.data.trx_no)) {
                    yoliPaiPayResult.data.amount = form.ReadOrDefault("price", 0m);
                    TradeAuthPaymentRequest tradeAuthPaymentRequest = new TradeAuthPaymentRequest {
                        Amount = (int)(yoliPaiPayResult.data.amount * 100m),
                        Authcode = form.ReadOrDefault("authCode")
                    };
                    string json;
                    if (form.TryGetValue("goods_detail", out json)) {
                        try {
                            tradeAuthPaymentRequest.GoodsList = (from d in json.JsonDecode<YolipaiGoodsDetail[]>()
                                                                 select new TradeGoods {
                                                                     Id = d.goods_id,
                                                                     Name = d.goods_name,
                                                                     Price = (int)(d.price * 100m),
                                                                     Quantity = d.quantity
                                                                 }).ToArray<TradeGoods>();
                        }
                        catch {
                        }
                    }
                    hongshPayTrade = ctx.Execute(tradeAuthPaymentRequest);
                    yoliPaiPayResult.data.trx_no = hongshPayTrade.TradeId;
                }
                else {
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        TradeId = yoliPaiPayResult.data.trx_no
                    });
                }
                yoliPaiPayResult.data.create_time = hongshPayTrade.CreateTime.ToString("yyy-MM-dd HH:mm:ss");
                if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                    throw new Exception(hongshPayTrade.StateDescript);
                }
                if (hongshPayTrade.State == TradeState.成功) {
                    yoliPaiPayResult.data.transaction_pay_time = hongshPayTrade.UpdateTime.ToString("yyy-MM-dd HH:mm:ss");
                    yoliPaiPayResult.msg = "Success";
                }
                else {
                    yoliPaiPayResult.code = 1;
                    yoliPaiPayResult.msg = hongshPayTrade.StateDescript;
                    yoliPaiPayResult.data.status = 102;
                }
            }
            catch (Exception ex) {
                yoliPaiPayResult.code = -1;
                yoliPaiPayResult.msg = ex.Message;
                yoliPaiPayResult.data.status = 31018;
            }
            ctx.Result = new HttpJsonResult(yoliPaiPayResult);
        }

        // Token: 0x02000094 RID: 148
        private class YolipaiGoodsDetail {
            // Token: 0x17000254 RID: 596
            // (get) Token: 0x06000556 RID: 1366 RVA: 0x00005296 File Offset: 0x00003496
            // (set) Token: 0x06000557 RID: 1367 RVA: 0x0000529E File Offset: 0x0000349E
            public string goods_id { get; set; }

            // Token: 0x17000255 RID: 597
            // (get) Token: 0x06000558 RID: 1368 RVA: 0x000052A7 File Offset: 0x000034A7
            // (set) Token: 0x06000559 RID: 1369 RVA: 0x000052AF File Offset: 0x000034AF
            public string goods_name { get; set; }

            // Token: 0x17000256 RID: 598
            // (get) Token: 0x0600055A RID: 1370 RVA: 0x000052B8 File Offset: 0x000034B8
            // (set) Token: 0x0600055B RID: 1371 RVA: 0x000052C0 File Offset: 0x000034C0
            public decimal price { get; set; }

            // Token: 0x17000257 RID: 599
            // (get) Token: 0x0600055C RID: 1372 RVA: 0x000052C9 File Offset: 0x000034C9
            // (set) Token: 0x0600055D RID: 1373 RVA: 0x000052D1 File Offset: 0x000034D1
            public double quantity { get; set; }

            // Token: 0x0600055E RID: 1374 RVA: 0x0000286D File Offset: 0x00000A6D
            public YolipaiGoodsDetail() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000095 RID: 149
        private class YoliPaiPayResultData {
            // Token: 0x17000258 RID: 600
            // (get) Token: 0x0600055F RID: 1375 RVA: 0x000052DA File Offset: 0x000034DA
            // (set) Token: 0x06000560 RID: 1376 RVA: 0x000052E2 File Offset: 0x000034E2
            public decimal amount { get; set; }

            // Token: 0x17000259 RID: 601
            // (get) Token: 0x06000561 RID: 1377 RVA: 0x000052EB File Offset: 0x000034EB
            // (set) Token: 0x06000562 RID: 1378 RVA: 0x000052F3 File Offset: 0x000034F3
            public int status { get; set; }

            // Token: 0x1700025A RID: 602
            // (get) Token: 0x06000563 RID: 1379 RVA: 0x000052FC File Offset: 0x000034FC
            // (set) Token: 0x06000564 RID: 1380 RVA: 0x00005304 File Offset: 0x00003504
            public string user_no { get; set; }

            // Token: 0x1700025B RID: 603
            // (get) Token: 0x06000565 RID: 1381 RVA: 0x0000530D File Offset: 0x0000350D
            // (set) Token: 0x06000566 RID: 1382 RVA: 0x00005315 File Offset: 0x00003515
            public string serialNum { get; set; }

            // Token: 0x1700025C RID: 604
            // (get) Token: 0x06000567 RID: 1383 RVA: 0x0000531E File Offset: 0x0000351E
            // (set) Token: 0x06000568 RID: 1384 RVA: 0x00005326 File Offset: 0x00003526
            public string order_no { get; set; }

            // Token: 0x1700025D RID: 605
            // (get) Token: 0x06000569 RID: 1385 RVA: 0x0000532F File Offset: 0x0000352F
            // (set) Token: 0x0600056A RID: 1386 RVA: 0x00005337 File Offset: 0x00003537
            public string trx_no { get; set; }

            // Token: 0x1700025E RID: 606
            // (get) Token: 0x0600056B RID: 1387 RVA: 0x00005340 File Offset: 0x00003540
            public string bankOrderNo {
                get {
                    return trx_no;
                }
            }

            // Token: 0x1700025F RID: 607
            // (get) Token: 0x0600056C RID: 1388 RVA: 0x00005340 File Offset: 0x00003540
            public string transaction_id {
                get {
                    return trx_no;
                }
            }

            // Token: 0x17000260 RID: 608
            // (get) Token: 0x0600056D RID: 1389 RVA: 0x00005348 File Offset: 0x00003548
            // (set) Token: 0x0600056E RID: 1390 RVA: 0x00005350 File Offset: 0x00003550
            public string transaction_pay_time { get; set; }

            // Token: 0x17000261 RID: 609
            // (get) Token: 0x0600056F RID: 1391 RVA: 0x00005359 File Offset: 0x00003559
            // (set) Token: 0x06000570 RID: 1392 RVA: 0x00005361 File Offset: 0x00003561
            public string create_time { get; set; }

            // Token: 0x06000571 RID: 1393 RVA: 0x0000286D File Offset: 0x00000A6D
            public YoliPaiPayResultData() {
                //Class15.cwcZ3SWztHroi();
                //base..ctor();
            }
        }

        // Token: 0x02000096 RID: 150
        private class YoliPaiPayResult {
            // Token: 0x17000262 RID: 610
            // (get) Token: 0x06000572 RID: 1394 RVA: 0x0000536A File Offset: 0x0000356A
            // (set) Token: 0x06000573 RID: 1395 RVA: 0x00005372 File Offset: 0x00003572
            public int code { get; set; }

            // Token: 0x17000263 RID: 611
            // (get) Token: 0x06000574 RID: 1396 RVA: 0x0000537B File Offset: 0x0000357B
            // (set) Token: 0x06000575 RID: 1397 RVA: 0x00005383 File Offset: 0x00003583
            public string msg { get; set; }

            // Token: 0x17000264 RID: 612
            // (get) Token: 0x06000576 RID: 1398 RVA: 0x0000538C File Offset: 0x0000358C
            public YoliPaiPayResultData data { get; }

            // Token: 0x06000577 RID: 1399 RVA: 0x00005394 File Offset: 0x00003594
            public YoliPaiPayResult() {
                //Class15.cwcZ3SWztHroi();
                data = new YoliPaiPayResultData();
                //base..ctor();
            }
        }
    }
}
